<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="hospitalId">
              <el-select v-model="filters.hospitalId" :clearable="true" filterable placeholder="医院名称">
                <el-option v-for="item in hospitalList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 展示列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="thirdName" label="第三方" align="center" width="200" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="hospitalName" label="医院名称" align="center" width="240" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="officeName" label="科室名称" align="center" width="240" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="compName" label="第三方医院名称" align="center" width="200" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="clientId" label="下载账号" align="center" width="240" :show-overflow-tooltip="true"></el-table-column>
        <!--<el-table-column
                    prop="clientGuid" label="下载密码" align="center" width="120" :formatter="formatPassword"></el-table-column >-->
        <el-table-column label="状态" width="120" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="left"></el-table-column>

        <el-table-column label="操作" width="180" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button v-has="permissionSearch" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <!--  -1.删除 0.禁用 1.启用-->
                <!-- 删除逻辑：没被删除，在禁用中 | 启用逻辑：没删除，在禁用中 | 禁用逻辑：在启用中未被删除-->
                <el-dropdown-item v-has="permissionState" v-if="scope.row.state === 0" @click.native="handleClickSuccess(scope.row, 1)">启用</el-dropdown-item>

                <el-dropdown-item v-has="permissionState" v-if="scope.row.state === 1" @click.native="handleClickDisable(scope.row, 0)">禁用</el-dropdown-item>

                <el-dropdown-item v-has="permissionEdit" v-if="scope.row.state === 1" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>

                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state === 0" @click.native="handleIsDelete(scope.row, -1)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import OrganApi from '@/api/organ/organ';
import HospitalApi from '@/api/institution/hospital';
export default {
  name: 'ThirdAuthManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      hospitalList: [],
      loadingIns: false,
      listLoading: true,
      filters: {
        hospitalId: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      // 权限设置
      permissionSearch: 'SYSTEM_MANAGER_ORGAN_THIRD_ACCOUNT_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_ORGAN_THIRD_ACCOUNT_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_ORGAN_THIRD_ACCOUNT_LIST_DELETE',
      permissionAdd: 'SYSTEM_MANAGER_ORGAN_THIRD_ACCOUNT_LIST_ADD',
      permissionState: 'SYSTEM_MANAGER_ORGAN_THIRD_ACCOUNT_LIST_STATE',
    };
  },
  created() {
    // 每次进入页面 触发一次activated方法
    let _this = this;
    _this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.listHospital();
      this.getListData();
    }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      // 生命周期方法 获取所有医院名以及id | 获取列表数据
      _this.listHospital();
      _this.getListData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    handleClickAdd() {
      // 增加跳转
      let _this = this;
      _this.$router.push({ path: '/organ/thirdAccountAdd' });
    },
    formatState(cellValue) {
      if (cellValue === -1) {
        return '被删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue === -1) {
        return 'danger';
      } else if (cellValue === 0) {
        return 'info';
      } else if (cellValue === 1) {
        return 'success';
      }
    },
    handleClickEditor(row) {
      // 编辑
      let _this = this;
      _this.$router.push({
        path: '/organ/thirdAccountAdd',
        query: { accountId: row.accountId },
      });
    },
    handleIsDelete(row, state) {
      // 删除
      let _this = this;
      _this.$confirm('确认删除该账号', message.TITLE_PROMPT).then(() => {
        OrganApi.deleteThirdAccount({
          accountId: row.accountId,
          state: state,
        }).then((res) => {
          _this.$Utils.notifySuccess();
          _this.getListData();
        });
      });
    },
    handleClickSuccess(row, state) {
      // 启用
      let _this = this;
      _this.$confirm('确认启用该账号', message.TITLE_PROMPT).then(() => {
        OrganApi.deleteThirdAccount({
          accountId: row.accountId,
          state: state,
        }).then((res) => {
          _this.$Utils.notifySuccess();
          _this.getListData();
        });
      });
    },
    handleClickDisable(row, state) {
      // 禁用
      let _this = this;
      _this.$confirm('确认禁用该账号', message.TITLE_PROMPT).then(() => {
        OrganApi.deleteThirdAccount({
          accountId: row.accountId,
          state: state,
        }).then((res) => {
          _this.$Utils.notifySuccess();
          _this.getListData();
        });
      });
    },
    handleClickDetail(row) {
      // 查看详情
      let _this = this;
      _this.$router.push({
        path: '/organ/thirdAccountDetail',
        query: { accountId: row.accountId },
      });
    },
    onSearch() {
      // 查询
      this.listLoading = true;
      this.page.pageNo = 1;
      this.getListData();
    },
    resetForm() {
      // 重置
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.getListData();
    },
    handleCurrentChange(value) {
      let _this = this;
      _this.page.pageNo = value;
      _this.getListData();
    },
    listHospital(query) {
      // 根据获取所有的医院名称以及id
      let _this = this;
      _this.loadingIns = true;
      let cond = {
        hospitalType: 1,
      };
      HospitalApi.getHospitalNameList(cond)
        .then((res) => {
          setTimeout(() => {
            _this.loadingIns = false;
            _this.hospitalList = res.data.map((item) => {
              return {
                value: item.hospitalId,
                label: item.hospitalName,
              };
            });
          }, 200);
        })
        .catch((error) => {
          _this.hospitalList = [];
          console.error(error);
        });
    },
    getListData() {
      // 获取列表数据
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        hospitalId: _this.filters.hospitalId,
        // compName: _this.filters.compName,
        // clientId: _this.filters.clientId
      };
      OrganApi.listThirdAccount(cond).then((res) => {
        _this.listLoading = false;
        _this.dataList = res.data.list; //赋值
        _this.page.pageTotal = res.data.total;
      });
    },
    formatPassword(cellValue) {
      // 密码隐藏
      if (cellValue) {
        return '********';
      }
    },
  },
};
</script>

<style lang="scss" src="@/assets/css/views/content.scss" scop></style>
